package com.jsh.erp.service.transport;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.datasource.entities.Transport;
import com.jsh.erp.datasource.entities.Transport;
import com.jsh.erp.datasource.entities.TransportExample;
import com.jsh.erp.datasource.mappers.AccountHeadMapperEx;
import com.jsh.erp.datasource.mappers.TransportMapper;
import com.jsh.erp.datasource.mappers.TransportMapperEx;
import com.jsh.erp.datasource.mappers.DepotHeadMapperEx;
import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TransportService {
    private Logger logger = LoggerFactory.getLogger(TransportService.class);

    @Resource
    private TransportMapper transportMapper;

    @Resource
    private TransportMapperEx transportMapperEx;
    @Resource
    private UserService userService;
    @Resource
    private LogService logService;
    @Resource
    private SystemConfigService systemConfigService;

    public Transport getTransport(String id)throws Exception {
        Transport result=null;
        try{
            result=transportMapper.selectByPrimaryKey(id);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    public List<Transport> getTransportListByIds(String ids)throws Exception {
        List<String> idList = StringUtil.strToStringList(ids);
        List<Transport> list = new ArrayList<>();
        try{
            TransportExample example = new TransportExample();
            example.createCriteria().andIdIn(idList);
            list = transportMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public List<Transport> getTransport()throws Exception {
        TransportExample example = new TransportExample();
        example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<Transport> list=null;
        try{
            list=transportMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public List<Transport> select(String name, int offset, int rows)throws Exception {
        List<Transport> list=null;
        try{
            list=transportMapperEx.selectByConditionTransport(name, offset, rows);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public Long countTransport(String name, String type)throws Exception {
        Long result=null;
        try{
            result=transportMapperEx.countsByTransport(name);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertTransport(JSONObject obj, HttpServletRequest request)throws Exception {
        Transport transport = JSONObject.parseObject(obj.toJSONString(), Transport.class);
        transport.setUploadFlag("0");
        int result=0;
        try{
            result=transportMapper.insertSelective(transport);
            logService.insertLog("运输公司",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(transport.getTranspotName()).toString(), request);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateTransport(JSONObject obj, HttpServletRequest request)throws Exception {
        Transport transport = JSONObject.parseObject(obj.toJSONString(), Transport.class);
        transport.setUploadFlag("0");
        int result=0;
        try{
            result=transportMapper.updateByPrimaryKeySelective(transport);
            logService.insertLog("运输公司",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(transport.getTranspotName()).toString(), request);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deleteTransport(String id, HttpServletRequest request)throws Exception {
        return batchDeleteTransportByIds(id.toString());
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteTransport(String ids, HttpServletRequest request) throws Exception{
        return batchDeleteTransportByIds(ids);
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteTransportByIds(String ids)throws Exception {
        int result =0;
        String [] idArray=ids.split(",");
        //记录日志
        StringBuffer sb = new StringBuffer();
        sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
        List<Transport> list = getTransportListByIds(ids);
        for(Transport transport: list){
            sb.append("[").append(transport.getTranspotName()).append("]");
        }
        logService.insertLog("运输公司", sb.toString(),StringUtil.getRequest());
        //删除运输公司
        try{
            result=transportMapperEx.batchDeleteTransportByIds(idArray);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    public int checkIsNameExist(String id, String name) throws Exception{
        TransportExample example = new TransportExample();
        example.createCriteria().andIdNotEqualTo(id).andTranspotNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<Transport> list =null;
        try{
            list=transportMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list==null?0:list.size();
    }

    public Map<String,String> getTransportMap() throws Exception {
        List<Transport> bankList = getTransport();
        Map<String,String> bankMap = new HashMap<>();
        for(Transport bank : bankList){
            bankMap.put(bank.getId(), bank.getTranspotName());
        }
        return bankMap;
    }

    public String getTransportByMapAndIds(Map<String,String> bankMap, String bankIds)throws Exception {
        List<String> ids = StringUtil.strToStringList(bankIds);
        StringBuffer sb = new StringBuffer();
        for(String id: ids){
            sb.append(bankMap.get(id) + " ");
        }
        return sb.toString();
    }
}
